package cn.wsalix.spring;

import java.util.Calendar;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Environment;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import cn.wsalix.config.AppConfig;
import cn.wsalix.config.SysConfig;
import cn.wsalix.constant.Global;
import cn.wsalix.init.ProjectInit;
import cn.wsalix.msg.service.NoticeService;
import cn.wsalix.web.GlobalWeb;

@Component("beanDefineConfigue")
public class BeanDefineConfigue implements
		ApplicationListener<ContextRefreshedEvent> {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private String hwHello;
	@Autowired
	private SysConfig sysConfig;


	@Value("${urlSuffix}")
	private String urlSuffix;

	@Autowired
	private AppConfig appConfig;
	@Autowired
	private ProjectInit projectInit;
	@Autowired
	private NoticeService noticeService;

	@Autowired
	private LocalContainerEntityManagerFactoryBean entityManagerFactory;
	@Autowired
	private Global global;

	/**
	 * @param event
	 *            进入两次,event.getApplicationContext().getParent()一次为Null,<br>
	 *            完成后跳转 @WebInit 完成后跳回 再次进入，第二次不为Null,第二次可以获取RequestMapping
	 */
	@Transactional
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		if (event.getApplicationContext().getParent() == null) {
			// instanceof
			global.springContext = event.getApplicationContext();
			GlobalWeb.appConfig = appConfig;
			GlobalWeb.sysConfig = sysConfig;
			Global.urlSuffix = urlSuffix;
			Object[] arg = new Object[] { "Erica", Calendar.getInstance().getTime() }; 
			//String msg = Global.springContext.getMessage("userinfo", arg,Locale.CHINA); 

		} else {
			Boolean isTrue = (event.getApplicationContext() instanceof WebApplicationContext);
			logger.info("" + isTrue);

			Map<String, Object> jpaMap = entityManagerFactory
					.getJpaPropertyMap();
			boolean isCreate=jpaMap.get("hibernate.hbm2ddl.auto").equals("create");
			projectInit.initCheck(isCreate);
			if (isCreate) {
				
				// 数据初始化
				// 用户初始化
				// 菜单初始化
				// 角色初始化
				
				System.err.println("=====ContextRefreshedEvent====="
						+ event.getSource().getClass().getName());
				// projectInit.start();
			}
		}
		logger.info("==========finish");
	}

	private static StandardServiceRegistryImpl createServiceRegistry(
			Properties properties) {
		Environment.verifyProperties(properties);
		ConfigurationHelper.resolvePlaceHolders(properties);
		return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
				.applySettings(properties).build();
	}
}
